// games/cardGame.js

// 卡牌游戏的逻辑被封装在这里
const cardGame = {
  startGame: (room, io) => {
    console.log(`[卡牌游戏] 房间 ${room.id} 游戏开始！`);
    // 初始化游戏状态
    room.gameState = {
      deck: ['🂡', '🂢', '🂣', '🂤', '🂥'], // 示例牌组
      playerHands: {}
    };
    // 简单发牌
    room.players.forEach((player) => {
      room.gameState.playerHands[player.id] = [room.gameState.deck.pop()];
    });
    // 通知房间内所有玩家
    io.to(room.id).emit('game_started', {
      game: 'card_game',
      state: room.gameState
    });
  },

  handleAction: (room, socket, io, actionData) => {
    const { action, data } = actionData;
    switch (action) {
      case 'play_card':
        console.log(`[卡牌游戏] 玩家 ${socket.id} 出了一张牌: ${data.card}`);
        // 广播给其他玩家
        socket.to(room.id).emit('opponent_action', {
          playerId: socket.id,
          action: 'play_card',
          data: data
        });
        break;
      default:
        console.log(`[卡牌游戏] 未知动作: ${action}`);
        socket.emit('error', { message: '未知的卡牌游戏动作' });
    }
  }
};

// 导出这个模块，让其他文件可以 require 它
module.exports = cardGame;